package com.qqtech.qquan.order.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.activity.model.ActivityBaseVo;
import com.qqtech.qquan.activity.service.ActivityBaseService;
import com.qqtech.qquan.community.model.CommunityBase;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.coupon.service.CouponUserService;
import com.qqtech.qquan.delivery.service.DeliveryOrderService;
import com.qqtech.qquan.hi.service.HiOrderService;
import com.qqtech.qquan.homemaking.service.HomemakingOrderService;
import com.qqtech.qquan.mall.enums.MallShopTypeDictEnum;
import com.qqtech.qquan.mall.model.MallShop;
import com.qqtech.qquan.mall.model.MallShopQuery;
import com.qqtech.qquan.mall.model.MallShopVo;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.qquan.mall.service.MallSupplierUserService;
import com.qqtech.qquan.order.enums.OrderBaseDeliveryStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBaseOrderStatusCancelTypeDictEnum;
import com.qqtech.qquan.order.enums.OrderBaseOrderStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBaseOrderTypeDictEnum;
import com.qqtech.qquan.order.enums.OrderBasePayStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBasePayTypeDictEnum;
import com.qqtech.qquan.order.enums.OrderBaseReturnPayStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBaseSendDeliveryStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderDeliverySendDeliveryTypeDictEnum;
import com.qqtech.qquan.order.enums.OrderDeliveryTypeDictEnum;
import com.qqtech.qquan.order.model.OrderBase;
import com.qqtech.qquan.order.model.OrderBaseQuery;
import com.qqtech.qquan.order.model.OrderBaseVo;
import com.qqtech.qquan.order.model.OrderCouponQuery;
import com.qqtech.qquan.order.model.OrderCouponVo;
import com.qqtech.qquan.order.model.OrderDelivery;
import com.qqtech.qquan.order.model.OrderItem;
import com.qqtech.qquan.order.model.OrderItemQuery;
import com.qqtech.qquan.order.model.OrderItemVo;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.order.service.OrderCouponService;
import com.qqtech.qquan.order.service.OrderDeliveryService;
import com.qqtech.qquan.order.service.OrderHandleService;
import com.qqtech.qquan.order.service.OrderItemService;
import com.qqtech.qquan.order.service.OrderSysDictService;
import com.qqtech.qquan.pub.enums.PubFeedbackObjDictEnum;
import com.qqtech.qquan.pub.model.PubFeedback;
import com.qqtech.qquan.pub.model.PubFeedbackQuery;
import com.qqtech.qquan.pub.service.PubFeedbackService;
import com.qqtech.qquan.push.service.PushBizzService;
import com.qqtech.qquan.send.enums.SendDeliveryUserAuditStatusDictEnum;
import com.qqtech.qquan.send.model.SendDeliveryOrderVo;
import com.qqtech.qquan.send.model.SendDeliveryUser;
import com.qqtech.qquan.send.model.SendDeliveryUserOrder;
import com.qqtech.qquan.send.service.SendDeliveryOrderService;
import com.qqtech.qquan.send.service.SendDeliveryUserOrderService;
import com.qqtech.qquan.send.service.SendDeliveryUserService;
import com.qqtech.qquan.sms.service.SmsBizzService;
import com.qqtech.qquan.sys.enums.SysConst;
import com.qqtech.qquan.sys.model.SysAreaVo;
import com.qqtech.qquan.sys.service.SysAreaService;
import com.qqtech.qquan.user.enums.UserIdentityTypeDictEnum;
import com.qqtech.qquan.user.model.UserAuth;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.model.UserIdentity;
import com.qqtech.qquan.user.model.UserIdentityQuery;
import com.qqtech.qquan.user.service.UserAddressService;
import com.qqtech.qquan.user.service.UserAuthService;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserIdentityService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/order/base")
public class OrderBaseController extends BaseWebController<OrderBase, OrderBaseQuery> {
	@Resource
	private OrderBaseService orderBaseService;

	@Resource
	private OrderDeliveryService orderDeliveryService;

	@Resource
	private OrderItemService orderItemService;

	@Resource
	private UserBaseService userBaseService;

	@Resource
	private HomemakingOrderService homemakingOrderService;

	@Resource
	private OrderHandleService orderHandleService;

	@Resource
	private UserAddressService userAddressService;

	@Resource
	private DeliveryOrderService deliveryOrderService;

	@Resource
	private MallShopService mallShopService;

	@Resource
	private OrderSysDictService orderSysDictService;

	@Resource
	private UserIdentityService userIdentityService;

	@Resource
	private SendDeliveryUserService sendDeliveryUserService;

	@Resource
	private SendDeliveryOrderService sendDeliveryOrderService;

	@Resource
	private OrderCouponService orderCouponService;

	@Resource
	private MallSupplierUserService mallSupplierUserService;
	@Resource
	private PushBizzService pushBizzService;
	@Resource
	private SendDeliveryUserOrderService sendDeliveryUserOrderService;
	@Resource
	private PubFeedbackService pubFeedbackService;
	@Resource
	private UserAuthService userAuthService;
	@Resource
	private SmsBizzService smsBizzService;
	@Resource
	private ActivityBaseService activityBaseService;
	@Resource
	private HiOrderService hiOrderService;
	@Resource
	private CouponUserService couponUserService;

	@Resource
	private CommunityBaseService communityBaseService;
	
	@Resource
	private SysAreaService sysAreaService;
	
	@Override
	protected BaseService<OrderBase> getBaseService() {
		return orderBaseService;
	}

	/**
	 * 我的订单列表
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/page")
	@ResponseBody
	public Result page(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if (orderBaseQuery == null) {
			orderBaseQuery = new OrderBaseQuery();
		}
		if (orderBaseQuery.getBuyerId() == null) {
			orderBaseQuery.setBuyerId(super.getUserId());
		}
		Pageable pageKit = new PageKit(orderBaseQuery.getPage(), new PageOrder(Direction.DESC, "newAt"));
		Page<OrderBaseVo> page = orderBaseService.queryPageList(orderBaseQuery, pageKit);
		List<OrderBaseVo> list = page.getContent();
		int i = 0;
		if (orderBaseQuery.getPage() != null && orderBaseQuery.getPage() > 1) {
			i = (orderBaseQuery.getPage() - 1) * pageKit.getPageSize();
		} else {
			i = 0;
		}
		if (list != null && list.size() > 0) {
			for (OrderBaseVo order : list) {
				setSellerName(order);
				setOrderStatusText(order);
				order.setIndex(i);
				i++;
			}
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 卖方订单列表
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/sellerPage")
	@ResponseBody
	public Result sellerPage(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if (orderBaseQuery == null) {
			orderBaseQuery = new OrderBaseQuery();
		}
		orderBaseQuery.setSellerId(super.getUserId());
		Pageable pageKit = new PageKit(orderBaseQuery.getPage(), new PageOrder(Direction.DESC, "newAt"));
		Page<OrderBaseVo> page = orderBaseService.queryPageList(orderBaseQuery, pageKit);
		UserBaseVo userBaseVo = null;
		int i = 0;
		if (orderBaseQuery.getPage() != null && orderBaseQuery.getPage() > 1) {
			i = (orderBaseQuery.getPage() - 1) * pageKit.getPageSize();
		}
		for (OrderBaseVo orderBaseVo : page) {
			userBaseVo = userBaseService.getById(orderBaseVo.getBuyerId());
			orderBaseVo.setBuyerNick(userBaseVo.getNick());
			if (StringUtil.isNotBlank(userBaseVo.getLogo())) {
				orderBaseVo.setBuyerLogo(userBaseVo.getFullLogo());
			}
			setOrderStatusText(orderBaseVo);
			orderBaseVo.setIndex(i);
			i++;
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 圈商店铺订单列表
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/shopOrderPage")
	@ResponseBody
	public Result shopOrderPage(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if (orderBaseQuery == null) {
			orderBaseQuery = new OrderBaseQuery();
		}
		// 获取用户对应的店铺
		MallShopQuery query = new MallShopQuery();
		query.setCreatorId(super.getUserId());
		query.setType(MallShopTypeDictEnum.QQMALLSHOP.getCode());
		List<MallShopVo> mallShopList = mallShopService.queryList(query);
		MallShop ms = new MallShop();
		if (mallShopList != null && mallShopList.size() > 0) {
			ms = mallShopList.get(0);
		}
		if (ms != null && ms.getId() != null) {
			OrderBaseQuery orderBaseQuery2 = new OrderBaseQuery();
			orderBaseQuery2.setOrderStatus(OrderBaseOrderStatusDictEnum.COMPLETED.getCode());
			orderBaseQuery2.setOrderType(OrderBaseOrderTypeDictEnum.QSHOP.getCode());
			orderBaseQuery2.setShopId(ms.getId());
			// 圈商类型
			if (null != orderBaseQuery.getStartOrderStatusAt() && null != orderBaseQuery.getEndOrderStatusAt()) {
				orderBaseQuery2.setStartOrderStatusAt(orderBaseQuery.getStartOrderStatusAt());
				orderBaseQuery2.setEndOrderStatusAt(orderBaseQuery.getEndOrderStatusAt());
				orderBaseQuery.setOrderStatus(OrderBaseOrderStatusDictEnum.COMPLETED.getCode());
			}
			orderBaseQuery.setOrderType(OrderBaseOrderTypeDictEnum.QSHOP.getCode());
			orderBaseQuery.setShopId(ms.getId());
			Pageable pageKit = new PageKit(orderBaseQuery.getPage(), new PageOrder(Direction.DESC, "newAt"));
			Page<OrderBaseVo> page = orderBaseService.queryPageList(orderBaseQuery, pageKit);
			List<OrderBaseVo> list = page.getContent();
			int i = 0;
			if (orderBaseQuery.getPage() != null && orderBaseQuery.getPage() > 1) {
				i = (orderBaseQuery.getPage() - 1) * pageKit.getPageSize();
			}
			if (list != null && list.size() > 0) {
				for (OrderBaseVo order : list) {
					// setSellerName(order);
					if(order.getCommunityId()!=null){
						CommunityBase community = communityBaseService.getById(order.getCommunityId());
						if(community!=null){
							order.setCommunityName(community.getName());//订单-显示社区名称
						}
					}
					String buyerName = "";
					if (order.getBuyerId() != null && order.getBuyerId().intValue() != 0) {
						UserBase buyer = userBaseService.getById(order.getBuyerId());
						buyerName = buyer.getNick();
					}
					order.setBuyerNick(buyerName);
					setOrderStatusText(order);
					order.setShopStyle(ms.getStyle());
					order.setIndex(i);
					i++;
				}
			}
			int completedCount = orderBaseService.queryCount(orderBaseQuery2);
			BigDecimal sellerTotal = orderBaseService.getSumSellerTotal(orderBaseQuery2);
			result.put("completedCount", completedCount);
			result.put("sellerTotal", sellerTotal);
			result.put("page", page);
		}
		return result;
	}

	/**
	 * 待付款列表
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/waitPayPage")
	@ResponseBody
	public Result waitPayPage(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if (orderBaseQuery == null) {
			orderBaseQuery = new OrderBaseQuery();
		}
		orderBaseQuery.setBuyerId(super.getUserId());
		orderBaseQuery.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		orderBaseQuery.setPayStatus(OrderBasePayStatusDictEnum.UNPAYED.getCode());// 未付款
		Pageable pageKit = new PageKit(orderBaseQuery.getPage(), new PageOrder(Direction.DESC, "newAt"));
		Page<OrderBaseVo> page = orderBaseService.queryPageList(orderBaseQuery, pageKit);
		List<OrderBaseVo> list = page.getContent();
		if (list != null && list.size() > 0) {
			for (OrderBaseVo order : list) {
				setSellerName(order);
				setOrderStatusText(order);
			}
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 待发货列表
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/waitSendPage")
	@ResponseBody
	public Result waitSendPage(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if (orderBaseQuery == null) {
			orderBaseQuery = new OrderBaseQuery();
		}
		orderBaseQuery.setBuyerId(super.getUserId());
		orderBaseQuery.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		orderBaseQuery.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());// 已付款
		orderBaseQuery.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.UNSEND.getCode());// 未发货
		orderBaseQuery.setNotEqualReturnPayStatus(OrderBaseReturnPayStatusDictEnum.RETURNING.getCode());// 不等于退款中
		Pageable pageKit = new PageKit(orderBaseQuery.getPage(), new PageOrder(Direction.DESC, "newAt"));
		Page<OrderBaseVo> page = orderBaseService.queryPageList(orderBaseQuery, pageKit);
		List<OrderBaseVo> list = page.getContent();
		if (list != null && list.size() > 0) {
			for (OrderBaseVo order : list) {
				setSellerName(order);
				setOrderStatusText(order);
			}
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 待收货列表
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/waitReceiptPage")
	@ResponseBody
	public Result waitReceiptPage(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if (orderBaseQuery == null) {
			orderBaseQuery = new OrderBaseQuery();
		}
		orderBaseQuery.setBuyerId(super.getUserId());
		orderBaseQuery.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		orderBaseQuery.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());// 已付款
		orderBaseQuery.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.SENDED.getCode());// 商家已发货
		orderBaseQuery.setNotEqualReturnPayStatus(OrderBaseReturnPayStatusDictEnum.RETURNING.getCode());// 不等于退款中
		Pageable pageKit = new PageKit(orderBaseQuery.getPage(), new PageOrder(Direction.DESC, "newAt"));
		Page<OrderBaseVo> page = orderBaseService.queryPageList(orderBaseQuery, pageKit);
		List<OrderBaseVo> list = page.getContent();
		if (list != null && list.size() > 0) {
			for (OrderBaseVo order : list) {
				setSellerName(order);
				setOrderStatusText(order);
			}
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 待退款列表
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/waitRefundPage")
	@ResponseBody
	public Result waitRefundPage(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if (orderBaseQuery == null) {
			orderBaseQuery = new OrderBaseQuery();
		}
		orderBaseQuery.setBuyerId(super.getUserId());
		orderBaseQuery.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		orderBaseQuery.setReturnPayStatus(OrderBaseReturnPayStatusDictEnum.RETURNING.getCode());// 退款中
		Pageable pageKit = new PageKit(orderBaseQuery.getPage(), new PageOrder(Direction.DESC, "newAt"));
		Page<OrderBaseVo> page = orderBaseService.queryPageList(orderBaseQuery, pageKit);
		List<OrderBaseVo> list = page.getContent();
		if (list != null && list.size() > 0) {
			for (OrderBaseVo order : list) {
				setSellerName(order);
				setOrderStatusText(order);
			}
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 订单详情(根据订单id或者订单号查询)
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if (orderBaseQuery == null
				|| (orderBaseQuery.getId() == null && StringUtil.isBlank(orderBaseQuery.getOrderNo()))) {
			result.addErr("参数不能为空");
		}
		if (result.checkForm()) {
			OrderBaseVo order = null;
			if (orderBaseQuery.getId() != null) {
				order = orderBaseService.getById(orderBaseQuery.getId());
			} else {
				order = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
			}
			if (order != null) {
				if (order.getShopId() != null && order.getShopId().intValue() != 0) {// 店铺种类
					MallShop ms = mallShopService.getById(order.getShopId());
					if (ms != null && ms.getType().intValue() == MallShopTypeDictEnum.QQMALLSHOP.getCode()) {
						order.setShopStyle(ms.getStyle());
					}
				}

				List<OrderItem> itemList = orderItemService.findByOrderNo(order.getOrderNo());
				if (order.getOrderType().intValue() == OrderBaseOrderTypeDictEnum.HOMEMAKING.getCode()) {
					result.put("homemakingOrder", homemakingOrderService.getByOrderNo(order.getOrderNo()));
				} else if (order.getOrderType().intValue() == OrderBaseOrderTypeDictEnum.DELIVERY.getCode()) {
					result.put("deliveryOrder", deliveryOrderService.getByOrderNo(order.getOrderNo()));
				}
				OrderDelivery delivery = orderDeliveryService.getByOrderNo(order.getOrderNo());
				MallShop shop = null;
				String sellerName = "圈圈生活";
				String sellerPhone = ""; // 卖家电话
				if (OrderBaseOrderTypeDictEnum.QUANMALL.getCode() == order.getOrderType().intValue()
						|| OrderBaseOrderTypeDictEnum.QSHOP.getCode() == order.getOrderType().intValue()) {
					if (order.getShopId() != null) {
						shop = mallShopService.getById(order.getShopId());
						sellerName = shop.getName();
						sellerPhone = shop.getPhone();
					}
				} else {
					if (order.getSellerId() != null && order.getSellerId().intValue() != 0) {
						UserBase seller = userBaseService.getById(order.getSellerId());
						sellerName = seller.getNick();
						UserIdentityQuery uiq = new UserIdentityQuery();
						uiq.setSize(1);
						uiq.setUserId(order.getSellerId());
						uiq.setType(UserIdentityTypeDictEnum.PHONE.getCode());
						UserIdentity userIdentity = userIdentityService.getOne(uiq);
						if (userIdentity != null && userIdentity.getIdentity() != null) {
							sellerPhone = userIdentity.getIdentity();
						}
					}
				}
				OrderCouponQuery orderCouponQuery = new OrderCouponQuery();
				orderCouponQuery.setOrderNo(orderBaseQuery.getOrderNo());
				List<OrderCouponVo> orderCouponVoList = orderCouponService.queryList(orderCouponQuery);
				result.put("orderCouponVoList", orderCouponVoList);
				setOrderStatusText(order);
				result.put("order", order);
				result.put("itemList", itemList);
				result.put("delivery", delivery);
				result.put("sellerName", sellerName);
				result.put("sellerPhone", sellerPhone);
				result = getSendDelivery(order, delivery, shop, result);
			} else {
				result.addErr("没有数据");
			}
		}
		return result;
	}

	/**
	 * 获取订单配送信息
	 * 
	 * @param order
	 * @param result
	 * @return
	 */
	public Result getSendDelivery(OrderBaseVo order, OrderDelivery delivery, MallShop shop, Result result) {
		String deliveryType = "";
		String sendDeliveryName = "";// 配送员名称
		String sendDeliveryPhone = "";// 配送员电话
		if (order.getOrderType().intValue() == OrderBaseOrderTypeDictEnum.QUANMALL.getCode()
				|| order.getOrderType().intValue() == OrderBaseOrderTypeDictEnum.QSHOP.getCode()) {

			if (delivery.getType().intValue() == OrderDeliveryTypeDictEnum.SELLERSEND.getCode()) { // 非自提
				if (order.getDeliveryStatus().intValue() != OrderBaseDeliveryStatusDictEnum.UNSEND.getCode()) { // 已经发货
					if (delivery.getHasNeedDelivery().intValue() == YesNoEnum.NO.getCode()) { // 配送
						if (delivery.getSendDeliveryType().intValue() == OrderDeliverySendDeliveryTypeDictEnum.QUANQUAN
								.getCode()) {// 圈圈配送
							deliveryType = "圈圈配送";
							SendDeliveryUserOrder sendDeliveryUserOrder = sendDeliveryUserOrderService
									.getByOrderNo(order.getOrderNo());
							if (sendDeliveryUserOrder != null && sendDeliveryUserOrder.getDeliveryUserId() != null) {
								UserAuth sendDeliveryUserAuth = userAuthService
										.getByUserId(sendDeliveryUserOrder.getDeliveryUserId());
								if (sendDeliveryUserAuth != null) {
									sendDeliveryName = sendDeliveryUserAuth.getRealName();// 配送人员真实姓名
								}
								UserIdentityQuery uiq = new UserIdentityQuery();
								uiq.setSize(1);
								uiq.setUserId(sendDeliveryUserOrder.getDeliveryUserId());
								uiq.setType(UserIdentityTypeDictEnum.PHONE.getCode());
								UserIdentity userIdentity = userIdentityService.getOne(uiq);
								if (userIdentity != null && userIdentity.getIdentity() != null) {
									sendDeliveryPhone = userIdentity.getIdentity();
								}
							}
						} else { // 商家配送
							deliveryType = "商家配送";
							sendDeliveryName = shop.getName();
							sendDeliveryPhone = shop.getPhone();
						}
					} else {
						// 物流
					}
				} else {
					deliveryType = "配送";
				}
			} else if (delivery.getType().intValue() == OrderDeliveryTypeDictEnum.BUYERPICKUP.getCode()) { // 自提
				deliveryType = "买家自提";
			}
		}
		result.put("deliveryType", deliveryType);
		result.put("sendDeliveryName", sendDeliveryName);
		result.put("sendDeliveryPhone", sendDeliveryPhone);
		return result;
	}

	/**
	 * 订单详情(店铺)
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/shopOrderDetail")
	@ResponseBody
	public Result shopOrderDetail(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if (orderBaseQuery == null
				|| (orderBaseQuery.getId() == null && StringUtil.isBlank(orderBaseQuery.getOrderNo()))) {
			result.addErr("参数不能为空");
		}
		MallShopVo ms = mallShopService.getByCreatorId(super.getUserId());
		if (ms == null) {
			result.addErr("您还没有店铺");
		}
		if (result.checkForm()) {
			OrderBaseVo order = null;
			if (orderBaseQuery.getId() != null) {
				order = orderBaseService.getById(orderBaseQuery.getId());
			} else {
				order = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
			}
			if (order != null) {
				if(order.getCommunityId()!=null){
					CommunityBaseVo community = communityBaseService.getById(order.getCommunityId());
					if(community!=null){
						order.setCommunityName(community.getName());//订单-显示社区名称
					}
				}
				UserBaseVo userBaseVo = userBaseService.getById(order.getBuyerId());
				order.setBuyerNick(userBaseVo.getNick());
				List<OrderItem> itemList = orderItemService.findByOrderNo(order.getOrderNo());
				OrderDelivery delivery = orderDeliveryService.getByOrderNo(order.getOrderNo());
				OrderCouponQuery orderCouponQuery = new OrderCouponQuery();
				orderCouponQuery.setOrderNo(orderBaseQuery.getOrderNo());
				List<OrderCouponVo> orderCouponVoList = orderCouponService.queryList(orderCouponQuery);
				result.put("orderCouponVoList", orderCouponVoList);
				setOrderStatusText(order);
				result.put("order", order);
				result.put("itemList", itemList);
				result.put("delivery", delivery);
				result = getSendDelivery(order, delivery, ms, result);
			} else {
				result.addErr("没有数据");
			}
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/buyerTypeCount")
	@ResponseBody
	public Result buyerTypeCount() {
		Result result = new Result();
		OrderBaseQuery orderBaseQuery = new OrderBaseQuery();
		// 待付款
		orderBaseQuery.setBuyerId(super.getUserId());
		orderBaseQuery.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		orderBaseQuery.setPayStatus(OrderBasePayStatusDictEnum.UNPAYED.getCode());// 未付款
		int waitPayCount = orderBaseService.queryCount(orderBaseQuery);
		// 待退货
		orderBaseQuery.setReturnPayStatus(OrderBaseReturnPayStatusDictEnum.RETURNING.getCode());// 退款中
		int waitRefundCount = orderBaseService.queryCount(orderBaseQuery);
		// 待发货
		orderBaseQuery.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());// 已付款
		orderBaseQuery.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.UNSEND.getCode());// 未发货
		int waitSendCount = orderBaseService.queryCount(orderBaseQuery);
		// 待收货
		orderBaseQuery.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.SENDED.getCode());// 商家已发货
		int waitReceiptCount = orderBaseService.queryCount(orderBaseQuery);
		result.put("waitPayCount", waitPayCount);
		result.put("waitRefundCount", waitRefundCount);
		result.put("waitSendCount", waitSendCount);
		result.put("waitReceiptCount", waitReceiptCount);
		return result;
	}

	/**
	 * 圈商店铺订单各项数据
	 * 
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/shopOrderCount")
	@ResponseBody
	public Result shopOrderCount() {
		Result result = new Result();
		MallShop ms = mallShopService.getByCreatorId(super.getUserId());
		if (ms == null) {
			return result;
		}
		OrderBaseQuery orderBaseQuery = new OrderBaseQuery();
		orderBaseQuery.setShopId(ms.getId());
		orderBaseQuery.setSellerId(super.getUserId());
		orderBaseQuery.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		// 待退款
		orderBaseQuery.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());// 已付款
		orderBaseQuery.setReturnPayStatus(OrderBaseReturnPayStatusDictEnum.RETURNING.getCode());// 退款中
		int waitRefundCount = orderBaseService.queryCount(orderBaseQuery);
		// 待发货
		orderBaseQuery.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());// 已付款
		orderBaseQuery.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.UNSEND.getCode());// 未发货
		orderBaseQuery.setReturnPayStatus(null);
		int waitSendCount = orderBaseService.queryCount(orderBaseQuery);
		// 待收货
		orderBaseQuery.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.SENDED.getCode());// 商家已发货
		int waitReceiptCount = orderBaseService.queryCount(orderBaseQuery);
		result.put("waitRefundCount", waitRefundCount);
		result.put("waitSendCount", waitSendCount);
		result.put("waitReceiptCount", waitReceiptCount);
		return result;
	}

	/**
	 * 圈商店铺订单未完成数量
	 * 
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/shopOrderUncomplated")
	@ResponseBody
	public Result shopOrderUncomplated() {
		Result result = new Result();
		MallShop ms = mallShopService.getByCreatorId(super.getUserId());
		if (ms == null) {
			return result;
		}
		OrderBaseQuery orderBaseQuery = new OrderBaseQuery();
		orderBaseQuery.setShopId(ms.getId());
		orderBaseQuery.setSellerId(super.getUserId());
		orderBaseQuery.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());// 已付款
		orderBaseQuery.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		int uncompletedCount = orderBaseService.queryCount(orderBaseQuery);
		result.put("uncompletedCount", uncompletedCount);
		return result;
	}

	/**
	 * 订单详情(根据订单id或者订单号查询)
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/sellDetail")
	@ResponseBody
	public Result sellDetail(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if (orderBaseQuery == null
				|| (orderBaseQuery.getId() == null && StringUtil.isBlank(orderBaseQuery.getOrderNo()))) {
			result.addErr("参数不能为空");
		}
		if (result.checkForm()) {
			OrderBaseVo order = null;
			if (orderBaseQuery.getId() != null) {
				order = orderBaseService.getById(orderBaseQuery.getId());
			} else {
				order = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
			}
			if (order != null) {
				List<OrderItem> itemList = orderItemService.findByOrderNo(order.getOrderNo());
				int deliveryType = OrderDeliveryTypeDictEnum.SELLERSEND.getCode();
				if (order.getOrderType().intValue() == OrderBaseOrderTypeDictEnum.HOMEMAKING.getCode()) {
					deliveryType = OrderDeliveryTypeDictEnum.SELLERSERVICE.getCode();
					result.put("homemakingOrder", homemakingOrderService.getByOrderNo(order.getOrderNo()));
				} else if (order.getOrderType().intValue() == OrderBaseOrderTypeDictEnum.DELIVERY.getCode()) {
					deliveryType = OrderDeliveryTypeDictEnum.SELLERSERVICE.getCode();
					result.put("deliveryOrder", deliveryOrderService.getByOrderNo(order.getOrderNo()));
				}
				OrderDelivery delivery = orderDeliveryService.getByOrderNoAndType(order.getOrderNo(), deliveryType);
				String buyerName = "";
				if (order.getBuyerId() != null && order.getBuyerId().intValue() != 0) {
					UserBase buyer = userBaseService.getById(order.getBuyerId());
					buyerName = buyer.getNick();
				}
				String buyerPhone = "";
				UserIdentityQuery uiq = new UserIdentityQuery();
				uiq.setSize(1);
				uiq.setUserId(order.getBuyerId());
				uiq.setType(UserIdentityTypeDictEnum.PHONE.getCode());
				UserIdentity userIdentity = userIdentityService.getOne(uiq);
				if (userIdentity != null && userIdentity.getIdentity() != null) {
					buyerPhone = userIdentity.getIdentity();
				}
				setOrderStatusText(order);
				result.put("buyerPhone", buyerPhone);
				result.put("order", order);
				result.put("itemList", itemList);
				result.put("delivery", delivery);
				result.put("buyerName", buyerName);
			} else {
				result.addErr("没有数据");
			}
		}
		return result;
	}

	/**
	 * 
	 * @param order
	 * @param sellerId
	 *            null显示卖家，not null显示买家
	 */
	private void setSellerName(OrderBaseVo order) {
		// 卖家名称
		if (OrderBaseOrderTypeDictEnum.QUANMALL.getCode() == order.getOrderType().intValue()) {
			MallShop shop = mallShopService.getById(order.getShopId());
			order.setSellerNick(shop.getName());
		} else if (order.getSellerId() != null && order.getSellerId().intValue() != 0) {
			UserBase seller = userBaseService.getById(order.getSellerId());
			order.setSellerNick(seller.getNick());
		} else {
			order.setSellerNick("圈圈");
		}
	}

	/**
	 * 显示状态
	 * 
	 * @param order
	 */
	private void setOrderStatusText(OrderBaseVo order) {
		order.setHasBtn(false);
		order.setShowCancel(false);
		order.setCancelEnable(false);
		order.setShowPay(false);
		order.setShowComplete(false);
		order.setShowFeedback(false);
		order.setShowDelivery(false);
		order.setShowReturnAudit(false);
		order.setShowReturnPay(false);
		String orderStatusText = "";
		if (OrderBaseOrderStatusDictEnum.CANCEL.getCode() == order.getOrderStatus().intValue()) {
			orderStatusText = "已取消";
			if (OrderBaseOrderStatusCancelTypeDictEnum.BUYERCANCEL.getCode() == order.getOrderStatusCancelType()
					.intValue()) {
				orderStatusText = "买家已取消";
			} else if (OrderBaseOrderStatusCancelTypeDictEnum.SELLERCANCEL.getCode() == order.getOrderStatusCancelType()
					.intValue()) {
				orderStatusText = "卖家已取消";
			}
			if (OrderBaseReturnPayStatusDictEnum.RETURNED.getCode() == order.getReturnPayStatus().intValue()) {
				orderStatusText = "已退款";
			}
			order.setOrderStatusText(orderStatusText);
		} else if (OrderBaseOrderStatusDictEnum.DELETE.getCode() == order.getOrderStatus().intValue()) {
			order.setOrderStatusText("已删除");
		} else if (OrderBaseOrderStatusDictEnum.COMPLETED.getCode() == order.getOrderStatus().intValue()) {
			order.setOrderStatusText("交易完成");
			if (order.getCommentStatus().intValue() != 3) {
				order.setHasBtn(true);
				order.setShowFeedback(true);
			}
		} else {
			order.setHasBtn(true);
			if (OrderBasePayTypeDictEnum.ONLINE.getCode() == order.getPayType()
					&& OrderBasePayStatusDictEnum.UNPAYED.getCode() == order.getPayStatus()
					&& OrderBaseDeliveryStatusDictEnum.UNSEND.getCode() == order.getDeliveryStatus().intValue()
					&& OrderBaseReturnPayStatusDictEnum.NONEED.getCode() == order.getReturnPayStatus().intValue()) {
				order.setOrderStatusText("待付款");
				order.setShowCancel(true);
				order.setCancelEnable(true);
				order.setShowPay(true);
			} else if (OrderBasePayTypeDictEnum.ONLINE.getCode() == order.getPayType()
					&& OrderBasePayStatusDictEnum.PAYED.getCode() == order.getPayStatus()
					&& OrderBaseDeliveryStatusDictEnum.UNSEND.getCode() == order.getDeliveryStatus().intValue()) {
				orderStatusText = "待发货";
				order.setShowDelivery(true);
				if (order.getOrderType().intValue() == OrderBaseOrderTypeDictEnum.HOMEMAKING.getCode()) {// 服务订单
					orderStatusText = "待服务";
					order.setShowCancel(true);
				} else if (order.getOrderType().intValue() == OrderBaseOrderTypeDictEnum.ACTIVITY.getCode()) {
					OrderItemQuery query = new OrderItemQuery();
					query.setOrderNo(order.getOrderNo());
					OrderItemVo orderItemVo = orderItemService.getOne(query, new PageOrder());
					ActivityBaseVo activityBaseVo = activityBaseService.getById(orderItemVo.getProductId());
					if (activityBaseVo != null) {
						if (activityBaseVo.getSignEndAt() != null
								&& TimeUtil.now().before(activityBaseVo.getSignEndAt())) {// 未到报名截止时间
							order.setShowCancel(true);
						}
					}
				} else {
					order.setShowReturnPay(true);
					if (OrderBaseReturnPayStatusDictEnum.RETURNING.getCode() == order.getReturnPayStatus().intValue()) {
						orderStatusText = "申请退款";
						order.setShowReturnAudit(true);// 店铺订单退款审核
						order.setShowDelivery(false); // 店铺订单发货入口关闭
						order.setShowReturnPay(false);// 买家关闭退款入口
					} else if (OrderBaseReturnPayStatusDictEnum.REFUSE.getCode() == order.getReturnPayStatus()
							.intValue()) {
						orderStatusText = "拒绝退款";
					}
				}
				order.setOrderStatusText(orderStatusText);
			} else if (OrderBasePayTypeDictEnum.ONDELIVERY.getCode() == order.getPayType()
					&& OrderBaseDeliveryStatusDictEnum.UNSEND.getCode() == order.getDeliveryStatus().intValue()) {
				order.setOrderStatusText("待发货");
				order.setShowDelivery(true);
				order.setShowCancel(true);
				order.setCancelEnable(true);
			} else if (OrderBasePayTypeDictEnum.ONLINE.getCode() == order.getPayType()
					&& OrderBasePayStatusDictEnum.PAYED.getCode() == order.getPayStatus()
					&& OrderBaseDeliveryStatusDictEnum.SENDED.getCode() == order.getDeliveryStatus().intValue()) {
				orderStatusText = "待签收";
				if (OrderBaseOrderTypeDictEnum.HOMEMAKING.getCode() == order.getOrderType().intValue()) {
					orderStatusText = "待确认服务";
				} else if (OrderBaseOrderTypeDictEnum.QSHOP.getCode() == order.getOrderType().intValue()) {
					OrderDelivery delivery = orderDeliveryService.getByOrderNo(order.getOrderNo());
					if (delivery.getType().intValue() == OrderDeliveryTypeDictEnum.SELLERSEND.getCode()) {
						// 卖家发货 && 不需要物流 && 圈圈配送
						if (delivery.getHasNeedDelivery().intValue() == YesNoEnum.NO.getCode()
								&& delivery.getSendDeliveryType()
										.intValue() == OrderDeliverySendDeliveryTypeDictEnum.QUANQUAN.getCode()) {
							if (OrderBaseSendDeliveryStatusDictEnum.NOTDELIVERY.getCode() == order
									.getSendDeliveryStatus().intValue()) {
								orderStatusText = "待配送";
							} else if (OrderBaseSendDeliveryStatusDictEnum.DELIVERYING.getCode() == order
									.getSendDeliveryStatus().intValue()) {
								orderStatusText = "配送中";
							} else {
								orderStatusText = "已配送";
							}
						}
					}
				}
				if (OrderBaseOrderTypeDictEnum.QUANMALL.getCode() == order.getOrderType().intValue()
						|| OrderBaseOrderTypeDictEnum.QSHOP.getCode() == order.getOrderType().intValue()) {
					order.setShowReturnPay(true);// 买家打开退款入口
					if (OrderBaseReturnPayStatusDictEnum.RETURNING.getCode() == order.getReturnPayStatus().intValue()) {
						orderStatusText = "申请退款";
						order.setShowReturnAudit(true);// 店铺订单退款审核
						order.setShowDelivery(false); // 店铺订单发货入口关闭
						order.setShowReturnPay(false);// 买家关闭退款入口
					} else if (OrderBaseReturnPayStatusDictEnum.REFUSE.getCode() == order.getReturnPayStatus()
							.intValue()) {
						orderStatusText = "拒绝退款";
						order.setShowReturnPay(true);// 买家打开退款入口
					}
				} else if (OrderBaseOrderTypeDictEnum.ACTIVITY.getCode() == order.getOrderType().intValue()) {
					order.setShowCancel(true);
				}
				order.setOrderStatusText(orderStatusText);
				order.setShowComplete(true);
			} else if (OrderBasePayTypeDictEnum.ONDELIVERY.getCode() == order.getPayType()
					&& OrderBaseDeliveryStatusDictEnum.SENDED.getCode() == order.getDeliveryStatus().intValue()) {
				if (OrderBaseOrderTypeDictEnum.HOMEMAKING.getCode() == order.getOrderType().intValue()) {
					order.setOrderStatusText("待确认服务");
				} else {
					if (OrderBasePayStatusDictEnum.PAYED.getCode() == order.getPayStatus()) {
						order.setOrderStatusText("待签收");
					} else {
						order.setOrderStatusText("待付款签收");
					}
				}
				order.setShowComplete(true);
			} else if (OrderBaseDeliveryStatusDictEnum.SIGNED.getCode() == order.getDeliveryStatus().intValue()) {
				order.setOrderStatusText("已签收");
				order.setShowFeedback(true);
			}
		}
	}

	/**
	 * 配送人员获取对应的订单
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/deliveryPage")
	@ResponseBody
	public Result deliveryPage(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		Integer userId = super.getUserId();
		SendDeliveryUser sendDeliveryUser = sendDeliveryUserService.getByUserId(userId);
		if (sendDeliveryUser != null && sendDeliveryUser.getAuditStatus()
				.intValue() == SendDeliveryUserAuditStatusDictEnum.THROUGH.getCode()) {
			result.put("isSendDeliveryUser", 1);
			if (sendDeliveryUser.getParentId().intValue() == 0) {
				result.put("isParentSendDeliveryUser", 1);
			} else {
				result.put("isParentSendDeliveryUser", 0);
			}
			if (sendDeliveryUser.getParentUserId().intValue() == 0
					|| sendDeliveryUser.getHasSuperPower().intValue() == YesNoEnum.YES.getCode()) {
				result.put("canAssigned", 1); // 一级配送和超级权限的 可以分配订单
			} else {
				result.put("canAssigned", 0);
			}
			if (orderBaseQuery == null) {
				orderBaseQuery = new OrderBaseQuery();
			}
			if (orderBaseQuery.getParentSendDeliveryUser() != null
					&& orderBaseQuery.getParentSendDeliveryUser().intValue() == 1) {
				// 查询我指派的
				orderBaseQuery.setCommunityId(sendDeliveryUser.getCommunityId());
				orderBaseQuery.setParentSendDeliveryUser(sendDeliveryUser.getUserId());
			} else {
				orderBaseQuery.setHasSuperSendDelivery(sendDeliveryUser.getHasSuperPower());
				if (sendDeliveryUser.getHasSuperPower().intValue() == YesNoEnum.NO.getCode()) {
					// 不是超级权限的 只能查看自己的
					orderBaseQuery.setCommunityId(sendDeliveryUser.getCommunityId());
					orderBaseQuery.setSendDeliveryUserId(userId);
				}
			}
			Pageable pageKit = new PageKit(orderBaseQuery.getPage(), new PageOrder(Direction.DESC, "base.newAt"),
					new PageOrder(Direction.ASC, "od.address"));
			Page<OrderBaseVo> page = orderBaseService.queryPageBySendDeliveryUser(orderBaseQuery, pageKit);
			int index = 0;
			if (orderBaseQuery.getPage() != null && orderBaseQuery.getPage() > 1) {
				index = (orderBaseQuery.getPage() - 1) * pageKit.getPageSize();
			}
			UserBaseVo userBaseVo = null;
			for (OrderBaseVo orderBaseVo : page) {
				userBaseVo = userBaseService.getById(orderBaseVo.getBuyerId());
				orderBaseVo.setBuyerNick(userBaseVo.getNick());
				if (StringUtil.isNotBlank(userBaseVo.getLogo())) {
					orderBaseVo.setBuyerLogo(userBaseVo.getFullLogo());
				}
				if (orderBaseVo.getSendDeliveryStatus().intValue() == OrderBaseSendDeliveryStatusDictEnum.DELIVERYED
						.getCode()) {
					orderBaseVo.setHasBtn(false);
					orderBaseVo.setShowDelivery(false);
				} else {
					orderBaseVo.setHasBtn(true);
					orderBaseVo.setShowDelivery(true);
				}
				orderBaseVo.setIndex(index);
				index++;
				int deliveryType = OrderDeliveryTypeDictEnum.SELLERSEND.getCode();
				OrderDelivery delivery = orderDeliveryService.getByOrderNoAndType(orderBaseVo.getOrderNo(),deliveryType);
				orderBaseVo.setSendDeliveryUserName(
						"配送人员：" + orderBaseVo.getSendDeliveryUserName() + "\r\n买家地址：" + delivery.getAddress());
			}
			result.put("page", page);
			if (orderBaseQuery.getPage() != null && orderBaseQuery.getPage().intValue() == 1) {
				// 未配送的数量
				OrderBaseQuery sendDeliveryC = new OrderBaseQuery();
				sendDeliveryC.setHasSuperSendDelivery(sendDeliveryUser.getHasSuperPower());
				if (sendDeliveryUser.getHasSuperPower().intValue() == YesNoEnum.NO.getCode()) {
					// 不是超级权限的 只能查看自己的
					sendDeliveryC.setSendDeliveryUserId(userId);
				}
				sendDeliveryC.setHasSendDelivery(2);
				result.put("notSendDeliveryC", orderBaseService.queryCountBySendDeliveryUser(sendDeliveryC));
				// 已配送的数量
				sendDeliveryC.setHasSendDelivery(1);
				result.put("sendDeliveryC", orderBaseService.queryCountBySendDeliveryUser(sendDeliveryC));
				// 查询我指派的数量
				sendDeliveryC.setHasSendDelivery(null);
				sendDeliveryC.setSendDeliveryUserId(null);
				sendDeliveryC.setParentSendDeliveryUser(userId);
				result.put("parentSendDeliveryC", orderBaseService.queryCountBySendDeliveryUser(sendDeliveryC));
			}
		} else {
			result.put("isSendDeliveryUser", 0);
		}
		return result;
	}

	/**
	 * 配送人员把订单转给其他配送人员
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/changeDeliveryUserId")
	@ResponseBody
	public Result changeDeliveryUserId(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		OrderBaseVo order = null;
		if (orderBaseQuery == null) {
			result.addErr("参数不能为空");
		} else {
			if (StringUtil.isBlank(orderBaseQuery.getOrderNo())) {
				result.addErr("订单号不能为空");
			} else {
				order = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
				if (order == null) {
					result.addErr("订单号不存在");
				} else if (order.getSendDeliveryStatus().intValue() == OrderBaseSendDeliveryStatusDictEnum.DELIVERYED
						.getCode()) {
					result.addErr("该订单已配送");
				} else if (order.getSendDeliveryStatus().intValue() == OrderBaseSendDeliveryStatusDictEnum.DELIVERYING
						.getCode()) {
					result.addErr("该订单在配送中");
				}
			}
			if (result.checkForm() && orderBaseQuery.getSendDeliveryUserId() == null) {
				result.addErr("请选择配送人员");
			}
		}
		if (result.checkForm()) {
			SendDeliveryUserOrder sendDeliveryUserOrder = sendDeliveryUserOrderService
					.getByOrderNo(orderBaseQuery.getOrderNo());
			if (sendDeliveryUserOrder != null) {
				SendDeliveryUserOrder update = new SendDeliveryUserOrder();
				update.setId(sendDeliveryUserOrder.getId());
				update.setAssignedAt(TimeUtil.now());
				update.setAssignedUserId(super.getUserId());
				update.setDeliveryUserId(orderBaseQuery.getSendDeliveryUserId());
				sendDeliveryUserOrderService.updateById(update);
				smsBizzService.orderPayedToSender(order.getOrderNo());
				pushBizzService.sendOrderWhenPayed(order.getOrderNo());
			}
		}
		return result;
	}

	/**
	 * 配送人员查看订单详情
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/deliveryDetail")
	@ResponseBody
	public Result deliveryDetail(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if (orderBaseQuery == null
				|| (orderBaseQuery.getId() == null && StringUtil.isBlank(orderBaseQuery.getOrderNo()))) {
			result.addErr("参数不能为空");
		}
		if (result.checkForm()) {
			OrderBaseVo order = null;
			if (orderBaseQuery.getId() != null) {
				order = orderBaseService.getById(orderBaseQuery.getId());
			} else {
				order = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
			}
			if (order != null) {
				if(order.getCommunityId()!=null){
					CommunityBaseVo communityBaseVo = communityBaseService.getById(order.getCommunityId());
					if(communityBaseVo!=null){
						order.setCommunityName(communityBaseVo.getName());//订单社区名称
					}
				}
				
				SendDeliveryUser sendDeliveryUser = sendDeliveryUserService.getByUserId(super.getUserId());
				if (sendDeliveryUser == null) {
					result.addErr("您还没申请成为配送人员");
					return result;
				}
				if (sendDeliveryUser.getParentUserId().intValue() == 0
						|| sendDeliveryUser.getHasSuperPower().intValue() == YesNoEnum.YES.getCode()) {
					result.put("canAssigned", 1); // 一级配送和超级权限的 可以分配订单
				} else {
					result.put("canAssigned", 0);
				}
				List<SendDeliveryOrderVo> sendDeliveryOrderList = sendDeliveryOrderService
						.findByOrderNo(order.getOrderNo());
				if(sendDeliveryOrderList!=null && sendDeliveryOrderList.size()>0){
					CommunityBaseVo community = null;
					MallShopVo shop = null;
					for(SendDeliveryOrderVo sendDo : sendDeliveryOrderList){
						shop = mallShopService.getById(sendDo.getSupplierId());
						if(shop!=null){
							community = communityBaseService.getById(shop.getCommunityId());
							SysAreaVo area = sysAreaService.getById(community.getDistrictId());
							sendDo.setSupplierAddress(area.getNameAll()+" "+community.getName()+"("+sendDo.getSupplierAddress()+")");
						}
					}
				}
				int deliveryType = OrderDeliveryTypeDictEnum.SELLERSEND.getCode();
				OrderDelivery delivery = orderDeliveryService.getByOrderNoAndType(order.getOrderNo(), deliveryType);
				UserBaseVo userBaseVo = userBaseService.getById(order.getBuyerId());
				order.setBuyerNick(userBaseVo.getNick());
				if (StringUtil.isNotBlank(userBaseVo.getLogo())) {
					order.setBuyerLogo(userBaseVo.getFullLogo());
				}
				if (order.getBuyerId() != null) {
					String buyerPhone = "";
					UserIdentityQuery uiq = new UserIdentityQuery();
					uiq.setSize(1);
					uiq.setUserId(order.getBuyerId());
					uiq.setType(UserIdentityTypeDictEnum.PHONE.getCode());
					UserIdentity userIdentity = userIdentityService.getOne(uiq);
					if (userIdentity != null && userIdentity.getIdentity() != null) {
						buyerPhone = userIdentity.getIdentity();
					}
					result.put("buyerPhone", buyerPhone);
				}
				if (order.getSendDeliveryStatus() != null
						&& order.getSendDeliveryStatus() == OrderBaseSendDeliveryStatusDictEnum.DELIVERYED.getCode()) {
					PubFeedbackQuery pubFeedbackQuery = new PubFeedbackQuery();
					pubFeedbackQuery.setStatus(YesNoEnum.YES.getCode());
					pubFeedbackQuery.setOrderNo(order.getOrderNo());
					pubFeedbackQuery.setObj(PubFeedbackObjDictEnum.DELIVERY.getCode());
					List<PubFeedback> pubFeedbackList = pubFeedbackService.queryList(pubFeedbackQuery);
					if (pubFeedbackList != null && !pubFeedbackList.isEmpty()) {
						pubFeedbackList.get(0).setImgString(pubFeedbackList.get(0).getImg(), SysConst.IMG_POSTFIX);
						result.put("pubFeedback", pubFeedbackList.get(0));
					}
				}
				result.put("order", order);
				result.put("sendDeliveryOrderList", sendDeliveryOrderList);
				result.put("delivery", delivery);
			} else {
				result.addErr("没有数据");
			}
		}
		return result;
	}

	/**
	 * 我的订单数量【待付款、待发货、待收货、待退款】
	 * 
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/myOrderCount")
	@ResponseBody
	public Result myOrderCount() {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		// 待付款
		OrderBaseQuery order1 = new OrderBaseQuery();
		order1.setBuyerId(curUserId);
		order1.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		order1.setPayStatus(OrderBasePayStatusDictEnum.UNPAYED.getCode());// 未付款
		int waitPay = orderBaseService.queryCount(order1);
		result.put("waitPayCount", waitPay);

		// 待发货
		OrderBaseQuery order2 = new OrderBaseQuery();
		order2.setBuyerId(curUserId);
		order2.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		order2.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());// 已付款
		order2.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.UNSEND.getCode());// 未发货
		order2.setNotEqualReturnPayStatus(OrderBaseReturnPayStatusDictEnum.RETURNING.getCode());// 不等于退款中
		int waitSend = orderBaseService.queryCount(order2);
		result.put("waitSendCount", waitSend);

		// 待收货
		OrderBaseQuery order3 = new OrderBaseQuery();
		order3.setBuyerId(curUserId);
		order3.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		order3.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());// 已付款
		order3.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.SENDED.getCode());// 商家已发货
		order3.setNotEqualReturnPayStatus(OrderBaseReturnPayStatusDictEnum.RETURNING.getCode());// 不等于退款中
		int waitReceipt = orderBaseService.queryCount(order3);
		result.put("waitReceiptCount", waitReceipt);

		// 待退款
		OrderBaseQuery order4 = new OrderBaseQuery();
		order4.setBuyerId(curUserId);
		order4.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		order4.setReturnPayStatus(OrderBaseReturnPayStatusDictEnum.RETURNING.getCode());// 退款中
		int waitReturn = orderBaseService.queryCount(order4);
		result.put("waitReturnCount", waitReturn);

		return result;
	}

	/**
	 * 订单详情(订单号查询)
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/detailByOrderNo")
	@ResponseBody
	public Result detailByOrderNo(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		OrderBaseVo detail = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
		result.put("detail", detail);
		return result;
	}

	/**
	 * 订单支付成功页面加载信息
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/orderSuccess")
	@ResponseBody
	public Result orderSuccess(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		OrderBaseVo orderBase = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
		// 荣誉系统，首次在平台下单成功（付款完成） +15荣誉值 +50圈币
		if (orderBase != null && orderBase.getPayStatus().intValue() == OrderBasePayStatusDictEnum.PAYED.getCode()
				&& (orderBase.getOrderType().intValue() == OrderBaseOrderTypeDictEnum.QUANMALL.getCode()
						|| orderBase.getOrderType().intValue() == OrderBaseOrderTypeDictEnum.QSHOP.getCode())) {
			hiOrderService.orderSuccess(super.getUserId(), orderBase.getId());
		}
		result.put("detail", orderBase);
		return result;
	}

	/**
	 * 扫 订单 二维码 完成自提订单
	 * 
	 * @param bizzId
	 *            订单号：orderNo
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/scan2Finish")
	@ResponseBody
	public Result scan2Finish(@RequestBody Map<String, String> map) {
		Result result = new Result();
		if (super.isVisitor()) {
			result.addErr("你还没有登陆!");
			return result;
		}
		String orderNo = map.get("bizzId").toString();
		Integer curUserId = super.getUserId();
		if (orderNo == null) {
			result.addErr("订单号为空!");
			return result;
		}
		OrderDelivery orderDelivery = orderDeliveryService.getByOrderNo(orderNo);
		if (orderDelivery != null) {
			if (orderDelivery.getType() != OrderDeliveryTypeDictEnum.BUYERPICKUP.getCode()) {
				result.addErr("非自提订单，不支持扫码！");
				return result;
			}
		} else {
			result.addErr("查询不到订单的配送方式！");
			return result;
		}
		OrderBaseVo orderBase = orderBaseService.getByOrderNo(orderNo);

		if (orderBase != null) {
			if (curUserId.intValue() != orderBase.getSellerId()) {
				result.addErr("你不是该订单的卖家！");
				return result;
			}
			if (orderBase.getPayStatus() != OrderBasePayStatusDictEnum.PAYED.getCode()) {
				result.addErr("订单未支付！");
				return result;
			}
			if (orderBase.getOrderStatus() == OrderBaseOrderStatusDictEnum.COMPLETED.getCode()) {
				result.addErr("订单已完成,商品已经被提走！");
				return result;
			}
		} else {
			result.addErr("查询不到该订单！");
			return result;
		}
		result = orderHandleService.buyerPickUpAutoSign(orderNo);// 在线支付订单，买家自提自动签收
		return result;
	}

	/**
	 * 我的优惠券数
	 * 
	 * @param
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/myCouponCount")
	@ResponseBody
	public Result myCouponCount() {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		Integer myCouponCount = couponUserService.queryCouponCount(curUserId);
		result.put("myCouponCount", myCouponCount);
		return result;
	}
}
